package com.hdsx.jxcsxm.operate.controller;

import com.hdsx.jxcsxm.operate.bean.ModuleBean;
import com.hdsx.jxcsxm.operate.bean.OperateBean;
import com.hdsx.jxcsxm.operate.bean.OperateParameter;
import com.hdsx.jxcsxm.operate.bean.ZjBean;
import com.hdsx.jxcsxm.operate.server.OperateServer;
import com.hdsx.jxcsxm.tjbb.bean.ExcelData;
import com.hdsx.jxcsxm.utile.EasyUIPage;
import com.hdsx.jxcsxm.utile.ExcelExportUtil;
import com.hdsx.jxcsxm.utile.JsonUtils;
import com.hdsx.jxcsxm.utile.MyUtil;
import com.hdsx.webutil.struts.BaseActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 占贤银
 * time 2023/8/23 15:22
 * description 生命之灯因热情而点燃，生命之舟因拼搏而前行
 */
@Controller
@Scope("prototype")
public class OperateController extends BaseActionSupport implements ModelDriven<OperateParameter> {

    @Resource(name = "operateServerImpl")
    private OperateServer server;

    private OperateParameter operateParameter = new OperateParameter();

    @Override
    public OperateParameter getModel() {
        return operateParameter;
    }

    public void exportOperateList(){
        try {
            HttpServletResponse response = getresponse();// 获得一个HttpServletResponse
            ExcelData eldata = new ExcelData();// 创建一个类
            eldata.setTitleName("列表信息");// 设置第一行
            eldata.setSheetName("列表信息");// 设置sheeet名
            eldata.setFileName("列表信息");// 设置文件名
            eldata.setEt(ExcelExportUtil.generateExcelTitle("json/list.json","resultData"));
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            eldata.setEl1(this.server.exportOperateList(operateParameter));
            List<ExcelData> excelDataList = new ArrayList<ExcelData>();
            excelDataList.add(eldata);
            ExcelExportUtil.export(excelDataList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void queryOperateList() {
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            List<OperateBean> list = server.queryOperateList(operateParameter);
            int count = server.queryOperateCount(operateParameter);
            EasyUIPage<OperateBean> e=new EasyUIPage<OperateBean>();
            e.setRows(list);
            e.setTotal(count);

            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exportDuration() {
        try {
            HttpServletResponse response = getresponse();// 获得一个HttpServletResponse
            ExcelData eldata = new ExcelData();// 创建一个类
            eldata.setTitleName("时长信息");// 设置第一行
            eldata.setSheetName("时长信息");// 设置sheeet名
            eldata.setFileName("时长信息");// 设置文件名
            eldata.setEt(ExcelExportUtil.generateExcelTitle("json/duration.json","resultData"));
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            eldata.setEl1(this.server.exportDuration(operateParameter));
            List<ExcelData> excelDataList = new ArrayList<ExcelData>();
            excelDataList.add(eldata);
            ExcelExportUtil.export(excelDataList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getDurationList() {
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            List<OperateBean> list = server.getDurationList(operateParameter);
            int count = server.getDurationCount(operateParameter);
            EasyUIPage<OperateBean> e=new EasyUIPage<OperateBean>();
            e.setRows(list);
            e.setTotal(count);
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getXmdurTj() {
        try {
            OperateBean operateBean = server.getXmdurTj(operateParameter);
            JsonUtils.write(operateBean, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getOperaInfoList() {
        try {
            List<OperateBean> list = server.getOperaInfoList(operateParameter);
            int count = server.getOperaInfoCount(operateParameter);
            EasyUIPage<OperateBean> e=new EasyUIPage<OperateBean>();
            e.setRows(list);
            e.setTotal(count);
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getModuleBmbm () {
        try {
            List<ModuleBean> results = server.getModuleBmbm();
            JsonUtils.write(results, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getQueryxmlx(String bh,String name) {
        String result="";
        if (bh != null && !"".equals(bh)) {
            String[] s = bh.split(",");
            for (int i = 0; i < s.length; i++) {
                if(i==0) {
                    result += " and (" + name + " like '" + s[i] + "'";
                } else {
                    result += " or " + name + " like '" + s[i] + "'";
                }
            }
            result += ")";
        }
        return result;
    }

    /**
     * 数据填报及时性
     */
    public void queryDatatjsxList(){
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            operateParameter.setJsxz(getQueryxmlx(operateParameter.getJsxz(), "jsxz"));
            List<ZjBean> list = server.queryDatatjsxList(operateParameter);
            int count = server.queryDatatjsxCount(operateParameter);
            EasyUIPage<ZjBean> e=new EasyUIPage<ZjBean>();
            e.setRows(list);
            e.setTotal(count);
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exportDatatjsx() {
        try {
            // SjbbMessage
            HttpServletResponse response = getresponse();// 获得一个HttpServletResponse
            ExcelData eldata = new ExcelData();// 创建一个类
            eldata.setTitleName("数据填报滞后情况");// 设置第一行
            eldata.setSheetName("数据填报滞后情况");// 设置sheeet名
            eldata.setFileName("数据填报滞后情况");// 设置文件名
            eldata.setEt(ExcelExportUtil.generateExcelTitle("json/datatbjsx.json","resultData"));
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            operateParameter.setJsxz(getQueryxmlx(operateParameter.getJsxz(), "jsxz"));
            eldata.setEl1(this.server.exportDatatjsx(operateParameter));
            List<ExcelData> excelDataList = new ArrayList<ExcelData>();
            excelDataList.add(eldata);
            ExcelExportUtil.export(excelDataList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getBmbmTreeByXmlx() {
        try {
            List<ModuleBean> results = server.getBmbmTreeByXmlx(operateParameter.getFlag());
            JsonUtils.write(results, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 数据填报质量
     */
    public void queryDatatbzlList() {
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            List<ZjBean> list = server.queryDatatbzlList(operateParameter);
            int count = server.queryDatatbzlCount(operateParameter);
            EasyUIPage<ZjBean> e=new EasyUIPage<ZjBean>();
            e.setRows(list);
            e.setTotal(count);
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // todo 待完成的任务
    public void exportDataWzx() {
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            operateParameter.setJsxz(getQueryxmlx(operateParameter.getJsxz(), "jsxz"));
            HttpServletResponse response = getresponse();// 获得一个HttpServletResponse
            ExcelData eldata = new ExcelData();// 创建一个类
            eldata.setTitleName("数据完整性(下达数据和到位数据不一致)");// 设置第一行
            eldata.setSheetName("数据完整性(下达数据和到位数据不一致)");// 设置sheeet名
            eldata.setFileName("数据完整性(下达数据和到位数据不一致)");// 设置文件名
            eldata.setEt(ExcelExportUtil.generateExcelTitle("json/datatbjsx.json","resultData"));
            eldata.setEl1(this.server.exportDataWzx(operateParameter));
            List<ExcelData> excelDataList = new ArrayList<ExcelData>();
            excelDataList.add(eldata);
            ExcelExportUtil.export(excelDataList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 数据完整性
     */
    public void queryDataWzxList() {
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            operateParameter.setJsxz(getQueryxmlx(operateParameter.getJsxz(), "jsxz"));
            List<ZjBean> list = server.queryDataWzxList(operateParameter);
            int count = server.queryDataWzxCount(operateParameter);
            EasyUIPage<ZjBean> e=new EasyUIPage<ZjBean>();
            e.setRows(list);
            e.setTotal(count);
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exportDataJcwtb() {
        try {
            HttpServletResponse response = getresponse();// 获得一个HttpServletResponse
            ExcelData eldata = new ExcelData();// 创建一个类
            eldata.setTitleName("数据结存占比");// 设置第一行
            eldata.setSheetName("数据结存占比");// 设置sheeet名
            eldata.setFileName("数据结存占比");// 设置文件名
            eldata.setEt(ExcelExportUtil.generateExcelTitle("json/wcdata.json","resultData"));
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            eldata.setEl1(this.server.exportDataJcwtb(operateParameter));
            List<ExcelData> excelDataList = new ArrayList<ExcelData>();
            excelDataList.add(eldata);
            ExcelExportUtil.export(excelDataList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void queryDataJcwtbList() {
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            List<ZjBean> list = server.queryDataJcwtbList(operateParameter);
            // int count = server.queryDataJcwtbCount(operateParameter);
            EasyUIPage<ZjBean> e=new EasyUIPage<ZjBean>();
            e.setRows(list);
            e.setTotal(list.size());
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exportDatathlv() {
        try {
            HttpServletResponse response = getresponse();// 获得一个HttpServletResponse
            ExcelData eldata = new ExcelData();// 创建一个类
            eldata.setTitleName("数据退回率");// 设置第一行
            eldata.setSheetName("数据退回率");// 设置sheeet名
            eldata.setFileName("数据退回率");// 设置文件名
            eldata.setEt(ExcelExportUtil.generateExcelTitle("json/datathl.json","resultData"));
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            operateParameter.setJsxz(getQueryxmlx(operateParameter.getJsxz(), "jsxz"));
            eldata.setEl1(this.server.exportDatathlv(operateParameter));
            List<ExcelData> excelDataList = new ArrayList<ExcelData>();
            excelDataList.add(eldata);
            ExcelExportUtil.export(excelDataList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void queryDatathlvList(){
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            operateParameter.setJsxz(getQueryxmlx(operateParameter.getJsxz(), "jsxz"));
            List<ZjBean> list = server.queryDatathlvList(operateParameter);
            // int count = server.queryDatathlvCount(operateParameter);
            EasyUIPage<ZjBean> e=new EasyUIPage<ZjBean>();
            e.setRows(list);
            e.setTotal(list.size());
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exportDatasbbl() {
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            operateParameter.setJsxz(getQueryxmlx(operateParameter.getJsxz(), "jsxz"));
            HttpServletResponse response = getresponse();// 获得一个HttpServletResponse
            ExcelData eldata = new ExcelData();// 创建一个类
            eldata.setTitleName("数据上报比例");// 设置第一行
            eldata.setSheetName("数据上报比例");// 设置sheeet名
            eldata.setFileName("数据上报比例");// 设置文件名
            eldata.setEt(ExcelExportUtil.generateExcelTitle("json/datasbbl.json","resultData"));
            eldata.setEl1(this.server.exportDatasbbl(operateParameter));
            List<ExcelData> excelDataList = new ArrayList<ExcelData>();
            excelDataList.add(eldata);
            ExcelExportUtil.export(excelDataList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void queryDatasbblList() {
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            operateParameter.setJsxz(getQueryxmlx(operateParameter.getJsxz(), "jsxz"));
            List<ZjBean> list = server.queryDatasbblList(operateParameter);
            // int count = server.queryDatasbblCount(operateParameter);
            EasyUIPage<ZjBean> e=new EasyUIPage<ZjBean>();
            e.setRows(list);
            e.setTotal(list.size());
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exportDatatbAccuracy() {
        try {
            HttpServletResponse response = getresponse();// 获得一个HttpServletResponse
            ExcelData eldata = new ExcelData();// 创建一个类
            eldata.setTitleName("数据填报准确性（到位和拨付资金类型不对应的情况）");// 设置第一行
            eldata.setSheetName("数据填报准确性（到位和拨付资金类型不对应的情况）");// 设置sheeet名
            eldata.setFileName("数据填报准确性（到位和拨付资金类型不对应的情况）");// 设置文件名
            eldata.setEt(ExcelExportUtil.generateExcelTitle("json/datatbzl.json","resultData"));
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            operateParameter.setJsxz(getQueryxmlx(operateParameter.getJsxz(), "jsxz"));
            eldata.setEl1(this.server.exportDatatbAccuracy(operateParameter));
            List<ExcelData> excelDataList = new ArrayList<ExcelData>();
            excelDataList.add(eldata);
            ExcelExportUtil.export(excelDataList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void queryDatatbAccuracy() {
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            operateParameter.setJsxz(getQueryxmlx(operateParameter.getJsxz(), "jsxz"));
            List<ZjBean> list = server.queryDatatbAccuracy(operateParameter);
            int count = server.queryDatatbAccuracyCount(operateParameter);
            EasyUIPage<ZjBean> e=new EasyUIPage<ZjBean>();
            e.setRows(list);
            e.setTotal(count);
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exportLoginNum() {
        try {
            HttpServletResponse response = getresponse();// 获得一个HttpServletResponse
            ExcelData eldata = new ExcelData();// 创建一个类
            eldata.setTitleName("登录统计");// 设置第一行
            eldata.setSheetName("登录统计");// 设置sheeet名
            eldata.setFileName("登录统计");// 设置文件名
            eldata.setEt(ExcelExportUtil.generateExcelTitle("json/loginnum.json","resultData"));
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            eldata.setEl1(this.server.exportLoginNum(operateParameter));
            List<ExcelData> excelDataList = new ArrayList<ExcelData>();
            excelDataList.add(eldata);
            ExcelExportUtil.export(excelDataList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void queryLoginNum() {
        try {
            operateParameter.setGydwdm(MyUtil.getQueryTJDW(operateParameter.getGydwdm(), "gydwdm"));
            List<ZjBean> list = server.queryLoginNumList(operateParameter);
            int count = server.queryLoginNumCount(operateParameter);
            EasyUIPage<ZjBean> e=new EasyUIPage<ZjBean>();
            e.setRows(list);
            e.setTotal(count);
            JsonUtils.write(e, getresponse().getWriter());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
